package com.situ.company.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FmtRequest {
	
	public static <T> T parseModel(HttpServletRequest req,Class<T> clazz, Map<String, String> fields){
		T obj = null;
		try {
			obj = clazz.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			return null;
		}
		for(Entry<String, String> entry : fields.entrySet()){
			try {
				Field field = clazz.getDeclaredField(entry.getKey());
				field.setAccessible(true);
				field.set(obj, req.getParameter(entry.getValue()));
			} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return obj;
	}
	
	
	public static void out(HttpServletResponse resp, String result) throws IOException {
		PrintWriter out = resp.getWriter();
		out.append(result);// 返回结果
		out.flush();
		out.close();
		out = null;
	}

}
